import Vue from 'vue'
import { Message } from 'element-ui'
Vue.component(Message.name, Message)

var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = 'yyyy-MM-dd';

function padding(s, len) {
    var len = len - (s + '').length;
    for (var i = 0; i < len; i++) { s = '0' + s; }
    return s;
};

export default {
    getQueryStringByName: function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        var context = "";
        if (r != null)
            context = r[2];
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    },
    formatDate: {
        format: function(date, pattern) {
            pattern = pattern || DEFAULT_PATTERN;
            return pattern.replace(SIGN_REGEXP, function($0) {
                switch ($0.charAt(0)) {
                    case 'y':
                        return padding(date.getFullYear(), $0.length);
                    case 'M':
                        return padding(date.getMonth() + 1, $0.length);
                    case 'd':
                        return padding(date.getDate(), $0.length);
                    case 'w':
                        return date.getDay() + 1;
                    case 'h':
                        return padding(date.getHours(), $0.length);
                    case 'm':
                        return padding(date.getMinutes(), $0.length);
                    case 's':
                        return padding(date.getSeconds(), $0.length);
                }
            });
        },
        parse: function(dateString, pattern) {
            var matchs1 = pattern.match(SIGN_REGEXP);
            var matchs2 = dateString.match(/(\d)+/g);
            if (matchs1.length == matchs2.length) {
                var _date = new Date(1970, 0, 1);
                for (var i = 0; i < matchs1.length; i++) {
                    var _int = parseInt(matchs2[i]);
                    var sign = matchs1[i];
                    switch (sign.charAt(0)) {
                        case 'y':
                            _date.setFullYear(_int);
                            break;
                        case 'M':
                            _date.setMonth(_int - 1);
                            break;
                        case 'd':
                            _date.setDate(_int);
                            break;
                        case 'h':
                            _date.setHours(_int);
                            break;
                        case 'm':
                            _date.setMinutes(_int);
                            break;
                        case 's':
                            _date.setSeconds(_int);
                            break;
                    }
                }
                return _date;
            }
            return null;
        }
    },
    //ajax错误处理
    // catchError: function(error) {
    //     if (error.response) {
    //         switch (error.response.status) {
    //             case 400:
    //                 Vue.prototype.$message({
    //                     message: error.response.data.message || '请求参数异常',
    //                     type: 'error'
    //                 });
    //                 break;
    //             case 401:
    //                 sessionStorage.removeItem('user');
    //                 Vue.prototype.$message({
    //                     message: error.response.data.message || '密码错误或账号不存在！',
    //                     type: 'warning',
    //                     onClose: function() {
    //                         location.reload();
    //                     }
    //                 });
    //                 break;
    //             case 403:
    //                 Vue.prototype.$message({
    //                     message: error.response.data.message || '无访问权限，请联系企业管理员',
    //                     type: 'warning'
    //                 });
    //                 break;
    //             default:
    //                 Vue.prototype.$message({
    //                     message: error.response.data.message || '服务端异常，请联系技术支持',
    //                     type: 'error'
    //                 });
    //         }
    //     }
    //     return Promise.reject(error);
    // },

    //自定义http拦截器
    httpJesen: function(url, params, callback) {
        Vue.prototype.$http.post(url, params, { headers: { "Content-Type": "application/json" } })
            .then(
                (response) => {
                    if (response.data.status === 200) {
                        callback(response.data)
                    } else {
                        Vue.prototype.$message({
                            message: response.data.message,
                            type: 'warning'
                        });
                    }
                },
                (error) => {
                    // console.log(error)
                    // debugger
                    Vue.prototype.$message({
                        message: '服务端异常，请联系技术支持',
                        type: 'error'
                    });
                }
            );
    },
    httpJesenGet: function(url, params, callback) {
        Vue.prototype.$http.get(url, { params: params })
            .then(
                (response) => {
                    if (response.data.status === 200) {
                        callback(response.data)
                    } else {
                        Vue.prototype.$message({
                            message: response.data.message,
                            type: 'warning'
                        });
                    }
                },
                (error) => {
                    Vue.prototype.$message({
                        message: '服务端异常，请联系技术支持',
                        type: 'error'
                    });
                }
            );
    },
    httpJesenFormData: function(url, formData, config, callback) {
        const loading = Vue.prototype.$loading({
            lock: true,
            text: '正在上传 请稍候...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });

        Vue.prototype.$http.post(url, formData, config)
            .then(
                (response) => {
                    if (response.data.status === 200) {
                        loading.close();
                        callback(response.data)
                    } else {
                        loading.close();
                        Vue.prototype.$message({
                            message: response.data.message,
                            type: 'warning'
                        });
                    }
                },
                (error) => {
                    loading.close();
                    Vue.prototype.$message({
                        message: '服务端异常，请联系技术支持',
                        type: 'error'
                    });
                }
            );
    },
    //导出excel
    exportExcel: function(url, params, callback) {
        Vue.prototype.$http.post(url, params, { responseType: 'arraybuffer' })
            .then(
                (response) => {
                    if (response.status === 200) {
                        let blob = new Blob([response.data], { type: "application/vnd.ms-excel" });　　　
                        console.log(blob)　　　
                        let objectUrl = URL.createObjectURL(blob);　　　　　　
                        window.location.href = objectUrl;

                    } else {
                        Vue.prototype.$message({
                            message: response.message,
                            type: 'warning'
                        });
                    }
                },
                (error) => {
                    loading.close();
                    Vue.prototype.$message({
                        message: '服务端异常，请联系技术支持',
                        type: 'error'
                    });
                }
            );
    }
};